var baseUrl = 'http://192.168.0.158/serverapi/';
baseUrl = 'http://localhost:8080/user-serverapi/';
baseUrl = 'http://192.168.0.158:8080/serverapi/';
var userUrl = baseUrl + "infoUser/";
var roleUrl = baseUrl + "infoRole/";
var authUrl = baseUrl + "infoAuth/";
var userRoleUrl = baseUrl + "infoUserRole/";
var roleAuthUrl = baseUrl + "infoRoleAuth/";
angular.module('apiUtilApp', ['ngResource','ngCookies'])
	.factory('ServiceUtil', function($q, $resource, $cookieStore, ACTIONS,METHODS){
		var factory = {};
		factory.excute = function(url, param , method) {
			var deferred = $q.defer();
	        var promise = deferred.promise;
	        if( method != METHODS.login ){
	        	param.sessionId = $cookieStore.get("sessionId");
	        }
			var resource = $resource(url, param, ACTIONS);
			
	    	if(method == 'get'){
				resource.get(param, function(data) {
					deferred.resolve(data);
				}, function(error) {
					deferred.reject(error);
				});
			} else if(method == 'getCount'){
				resource.getCount(param, function(data) {
					deferred.resolve(data);
				}, function(error) {
					deferred.reject(error);
				});
			} else if(method == 'getList'){
				resource.getList(param, function(data) {
					deferred.resolve(data);
				}, function(error) {
					deferred.reject(error);
				});
			} else if(method == 'save'){
				resource.save(param, function(data) {
					deferred.resolve(data);
				}, function(error) {
					deferred.reject(error);
				});
			} else if(method == 'update'){
				resource.update(param, function(data) {
					deferred.resolve(data);
				}, function(error) {
					deferred.reject(error);
				});
			} else if(method == 'delete'){
				resource.delete(param, function(data) {
					deferred.resolve(data);
				}, function(error) {
					deferred.reject(error);
				});
			} else if(method == 'login'){
				resource.login(param, function(data) {
					deferred.resolve(data);
				}, function(error) {
					deferred.reject(error);
				});
			}else {
				resource.get(param, function(data) {
					deferred.resolve(data);
				}, function(error) {
					deferred.reject(error);
				});
			}
			return promise;
		};
		return factory;
	});

var apiQueryApp = angular.module('apiQueryApp', ['ngResource','ngCookies'])
	apiQueryApp.factory('InfoUserService',  function($http, $q, $resource, $cookieStore, ServiceUtil, ACTIONS, METHODS){
	  	var factory = {};
	    var param = {};
	    
	    initParam = function(){
	    	param = {
		    	"id":"",
		    	"pageNo":"",
		    	"pageSize":"",
		    	"userId":"",
		    	"userName":"",
		    	"email":"",
		    	"phoneNum":"",
		    	"sex":"",
		    	"address":"",
		    	"roleId":"",
				"desc":""
	   		}
	    }
	    
		factory.login = function(user) {
			return ServiceUtil.excute(userUrl +'login', user, METHODS.login);
		};
	
		factory.getUserList = function(page,queryParam) {
			initParam();
			param = {};
	        param.pageNo = page.pageNo;
	        param.pageSize = page.pageSize;
	        if(queryParam){
	        	param.userId = queryParam.userId;
	        	param.userName = queryParam.userName;
	        	param.email = queryParam.email;
	        	param.phoneNum = queryParam.phoneNum;
	        	param.sex = queryParam.sex;
	        }
			return ServiceUtil.excute(userUrl +'getList', param, METHODS.getList);
		};
		
		factory.getCount = function(queryParam) {
			initParam();
			param = {};
	        if(queryParam){
	        	param.userId = queryParam.userId;
		    	param.userName = queryParam.userName;
		    	param.email = queryParam.email;
		    	param.phoneNum = queryParam.phoneNum;
		    	param.sex = queryParam.sex;
	        }
	        return ServiceUtil.excute(userUrl +'count', param, METHODS.getCount);
		};
		
		factory.deleteById = function(id) {
			initParam();
			return ServiceUtil.excute(userUrl +'delete/' + id, param ,METHODS.delete);
		};
		
		factory.addUser = function(user) {
			initParam();
			param = {};
	        param.userId = user.userId;
	    	param.userName = user.userName;
	    	param.email = user.email;
	    	param.phoneNum = user.phoneNum;
	    	param.sex = user.sex;
	    	param.address = user.address;
			return ServiceUtil.excute(userUrl +'add', param ,METHODS.save);
		};
		
		factory.updateUser = function(user) {
			initParam();
			param = {};
	        param.sessionId = $cookieStore.get("sessionId");
	        param.id = user.id;
	        param.userId = user.userId;
	    	param.userName = user.userName;
	    	param.email = user.email;
	    	param.phoneNum = user.phoneNum;
	    	param.sex = user.sex;
	    	param.address = user.address;
	    	return ServiceUtil.excute(userUrl +'update', param ,METHODS.update);
		};
		return factory;
	});

	apiQueryApp.factory('InfoUserRoleService',  function($http, $q, $resource, $cookieStore, ServiceUtil, ACTIONS, METHODS){
	var factory = {};
	var param = {};
	initParam = function(){
		param = {
			
			"id":"",
			"userId":"",
			"roleId":"",
			"desc":""
		};	
	};
	factory.getUserRole = function(userId) {
		
		initParam();
		param = {};
        param.userId = userId;
		return ServiceUtil.excute(userRoleUrl +'getList', param, METHODS.getList);
	};
	
	factory.addUserRole = function(userRole) {
		initParam();
		param = {};
        param.id = userRole.id;
        param.userId = userRole.userId;
    	param.roleId = userRole.roleId;
    	param.desc = userRole.desc;
    	return ServiceUtil.excute(userRoleUrl + 'add', param ,METHODS.save);
	};
	
	factory.deleteById = function(userRoleId) {
		initParam();
		param = {};
		return ServiceUtil.excute(userRoleUrl +'delete/' + userRoleId, param ,METHODS.delete);
	};
	return factory;
});

apiQueryApp.factory('InfoRoleAuthService',  function($http, $q, $resource, $cookieStore, ServiceUtil, ACTIONS, METHODS){
	var factory = {};
	var param = {};
	initParam = function(){
		param = {
			
			"id":"",
			"authId":"",
			"roleId":"",
			"desc":""
		};	
	};
	factory.getRoleAuth = function(roleId) {
		initParam();
		param = {};
        param.roleId = roleId;
		return ServiceUtil.excute(roleAuthUrl +'getList', param, METHODS.getList);
	};
	
	factory.addRoleAuth = function(roleAuth) {
		initParam();
		param = {};
        param.id = roleAuth.id;
        param.authId = roleAuth.authId;
    	param.roleId = roleAuth.roleId;
    	param.desc = roleAuth.desc;
    	return ServiceUtil.excute(roleAuthUrl + 'add', param ,METHODS.save);
	};
	
	factory.deleteById = function(roleAuthId) {
		initParam();
		param = {};
		return ServiceUtil.excute(roleAuthUrl +'delete/' + roleAuthId, param ,METHODS.delete);
	};
	
	return factory;
});

	
	apiQueryApp.factory('infoRoleService',  function($http, $q, $resource, $cookieStore, ServiceUtil, ACTIONS, METHODS){
		var factory = {};
		var param = {};
		
		initParam = function(){
			param = {
				"pageNo":"",
		    	"pageSize":"",
				"id":"",
				"name":"",
				"code":"",
				"desc":""
			};	
		};
		
		
		factory.getRoleList = function(page,queryParam) {
			initParam();
			param = {};
	        param.pageNo = page.pageNo;
	        param.pageSize = page.pageSize;
	        if(queryParam){
	        	param.name = queryParam.name;
	        	param.code = queryParam.code;
	        }
			return ServiceUtil.excute(roleUrl +'getList', param, METHODS.getList);
		};
		
		factory.getCount = function(queryParam) {
			initParam();
			param = {};
	        if(queryParam){
	        	param.name = queryParam.name;
		    	param.code = queryParam.code;
	        }
	        return ServiceUtil.excute(roleUrl +'count', param, METHODS.getCount);
		};
		
		factory.deleteById = function(id) {
			initParam();
			param = {};
			return ServiceUtil.excute(roleUrl +'delete/' + id, param ,METHODS.delete);
		};
		
		factory.addRole = function(role) {
			initParam();
			param = {};
	        param.name = role.name;
	    	param.code = role.code;
	    	param.desc = role.desc;
			return ServiceUtil.excute(roleUrl +'add', param ,METHODS.save);
		}; 
		
		factory.updateRole = function(role) {
			initParam();
			param = {};
	        param.sessionId = $cookieStore.get("sessionId");
	        param.id = role.id;
	        param.name = role.name;
	    	param.code = role.code;
	    	param.desc = role.desc;
	    	return ServiceUtil.excute(roleUrl +'update', param ,METHODS.update);
		};
		return factory;
	});

	apiQueryApp.factory('infoAuthService',  function($http, $q, $resource, $cookieStore, ServiceUtil, ACTIONS, METHODS){
	  	var factory = {};
	    var param = {};
	    
	    initParam = function(){
			param = {};	
		};
	    
		
		factory.getAuthList = function(page,queryParam) {
			initParam();
			param = {};
	        param.pageNo = page.pageNo;
	        param.pageSize = page.pageSize;
	        if(queryParam){
	        	param.name = queryParam.name;
	        	param.code = queryParam.code;
	        }
			return ServiceUtil.excute(authUrl +'getList', param, METHODS.getList);
		};
		
		factory.getCount = function(queryParam) {
			initParam();
			param = {};
	        if(queryParam){
	        	param.name = queryParam.name;
	        	param.code = queryParam.code;
	        }
	        return ServiceUtil.excute(authUrl +'count', param, METHODS.getCount);
		};
		
		factory.deleteById = function(id) {
			initParam();
			param = {};
			return ServiceUtil.excute(authUrl +'delete/' + id, param ,METHODS.delete);
		};
		
		factory.addAuth = function(auth) {
			initParam();
			param = {};
	    	param.name = auth.name;
	    	param.code = auth.code;
	    	param.desc = auth.desc;
	    	param.sessionId = $cookieStore.get("sessionId");
			return ServiceUtil.excute(authUrl +'add', param ,METHODS.save);
		};
		
		factory.updateAuth = function(auth) {
			initParam();
			param = {};
	        param.id = auth.id;
	        param.name = auth.name;
	    	param.code = auth.code;
	    	param.desc = auth.desc;
	    	param.sessionId = $cookieStore.get("sessionId");
	    	return ServiceUtil.excute(authUrl +'update', param ,METHODS.update);
		};
		return factory;
	});